<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               :search.sync="search"
               @row-save="rowSave"
               @row-update="rowUpdate"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   v-if="permission.tenant_delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template ref="reference" slot-scope="{row}"
                slot="message">
        <el-popover  placement="bottom"
                  title="日志内容"
                  width="200"
                  trigger="hover"
                  :content=row.message>
          <span slot="reference">{{row.message.slice(0, 50)}}</span>
        </el-popover>
      </template>
<!--      用来放追踪id的细节-->
<!--      <template>-->
<!--        <el-table :data="tableData"  style="width: 100%">-->
<!--          <el-table-column prop="date" label="日期" width="180"></el-table-column>-->
<!--          <el-table-column prop="name" label="姓名" width="180"></el-table-column>-->
<!--          <el-table-column prop="address" label="地址"></el-table-column>-->
<!--        </el-table>-->
<!--      </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getPage, getDetail} from "@/api/search/syslog";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        search: {},
        query: {},
        selectionList: [],
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: false,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: false,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "时间",
              prop: "timestamp",
              type: 'datetime',
              valueFormat: "yyyy-MM-dd hh:mm:ss",
            },
            {
              label: "日志信息",
              prop: "message",
              slot: true,
              cell :true

            },
            {
              label: "日志级别",
              prop: "logLevel",
            },
            {
              label: "应用名",
              prop: "appName",
              search: true,
            //  全文搜索 queryStr=user-center
            //   日志信息  ueryStr=message:user-center
            //   日志级别  ueryStr=logLevel:user-center
            //   应用名称  ueryStr=appName:user-center
            //   类名  ueryStr=classname:user-center
            },
            {
              label: "追踪id",
              prop: "address",

            },
            {
              label: "spandId",
              prop: "domain"
            }
            ,
            {
              label: "类名",
              prop: "classname"
            },
            {
              label: "线程名",
              prop: "threadName"
            },
            {
              label: "服务ip",
              prop: "serverIp"
            }
            ,
            {
              label: "服务端口",
              prop: "serverPort"
            },
            {
              label: "pid",
              prop: "pid"
            },
            {label:"id",
            prop: "id"
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: true,
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        create(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data;
          this.page.total = data.count;
          this.data = data.data;
          this.loading = false;
        });
      }
    }
  };
</script>

<style>
</style>
